package com.masterclient.app.modules.shop.contract;

import android.widget.Toast;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.common.bean.base.CommonBaseBean;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.shop.beans.StoreBean;
import com.eoner.baselibrary.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/09
*/
public class StorePresenter extends StoreContract.Presenter{

    @Override
    public void getSeller( String id,int page,String sort,String is_dealer, String is_agent) {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().storeIndex(id, page+"",sort,is_dealer,is_agent), new Consumer<CommonBaseResponse<StoreBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<StoreBean> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.getSellerResult(bean.getData());
                }else {
                    mView.getSellerFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                mView.getSellerFailed("网络异常，请重试");
                super.accept(o);
            }
        }));
    }

    @Override
    public void collectShop(String id) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().addFavorite(String.valueOf(Utils.getTimeStamp()),id,"6"), new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.collectSuccess(true);
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        }));
    }

    @Override
    public void removeCollect(String id) {
        mRxManager.add(toSubscribe(RetrofitUtil.getInstance().removeFavorite(String.valueOf(Utils.getTimeStamp()),id,"6"), new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.collectSuccess(false);
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        }));
    }

    @Override
    public void pickUpSalesrule(String coupon_code,String parent_product_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("coupon_code",coupon_code);
            jsonObject.put("parent_product_id",parent_product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = RetrofitUtil2.getInstance().coupon_receive(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if ("0".equals(bean.getCode())){
                    mView.pickUpSalesruleSuccess(coupon_code,bean.getMsg());
                }else {
                    mView.pickUpSalesruleFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.pickUpSalesruleFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

}